package com.example.myblog.controller;

import com.example.myblog.entity.Tag;
import com.example.myblog.service.BlogService;
import com.example.myblog.service.TagService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;

import java.util.HashMap;
import java.util.List;

@Controller
public class TagShowController {
    @Autowired
    private TagService tagService;
    @Autowired
    private BlogService blogService;

    @GetMapping("tags/{id}")
    public String tags(@PathVariable Integer id,Model model,Integer page){
        page = page==null?0 : page;
        List<Tag> tags = tagService.listTopNum(100);
        id = id==-1?tags.get(0).getId() : id;
        model.addAttribute("tags",tags);
        model.addAttribute("activeTagId",id);
        HashMap<String,Object> map = new HashMap<>();
        map.put("blogTags",tagService.findById(id).getName());
        model.addAttribute("page",blogService.listByCondition(map,page));
        return "tags";
    }
}
